﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace QLXMDataAccessLayer.BindingLogic
{
    public class MasterLogic
    {
        protected DataSet ds = null;
        protected AppSettingsReader configurationAppSettings = null;
        protected MySqlConnection mySqlConn = null;
        protected MySqlTransaction mySqlTrans = null;
        protected MySqlDataAdapter mySqlDataAdapter = null;
        private MySqlCommandBuilder mySqlCmdBuilder = null;
        private MySqlCommand mySqlCmd = null;

        public DataSet MasterDataSet
        {
            get { return ds; }
        }

        public MasterLogic()
        {
            ds = new DataSet("Master");
            mySqlCmd = new MySqlCommand();
        }

        public MySqlConnection OpenConnection()
        {
            if (this.mySqlConn == null)
            {
                string connectionString = DBInformation.GetDBInformation();
                this.mySqlConn = new MySqlConnection(connectionString);
                mySqlCmd.Connection = this.mySqlConn;
            }
            if (this.mySqlConn.State == ConnectionState.Closed)
            {
                this.mySqlConn.Open();
            }
            return this.mySqlConn;
        }

        public void CloseConnection()
        {
            if (this.mySqlConn != null)
            {
                this.mySqlConn.Close();
                this.mySqlConn.Dispose();
            }
            if (this.mySqlTrans != null)
            {
                this.mySqlTrans.Dispose();
            }
            if (this.mySqlTrans != null)
            {
                this.mySqlTrans.Dispose();
            }
        }
        public void BeginTransaction()
        {
            if (this.mySqlConn != null && this.mySqlConn.State == ConnectionState.Open)
            {
                this.mySqlTrans = this.mySqlConn.BeginTransaction();
            }
        }
        public void Commit()
        {
            if (this.mySqlConn != null)
            {
                this.mySqlTrans.Commit();
            }
        }
        public void Rollback()
        {
            if (this.mySqlTrans != null)
            {
                try
                {
                    this.mySqlTrans.Rollback();
                }
                catch
                {
                }
            }
        }


        public void ClearTableRows(string tableName)
        {
            if (ds.Tables[tableName] != null)
            {
                ds.Tables[tableName].Rows.Clear();
            }
        }

        public void ClearTableRows()
        {
            ds.Clear();
        }

        public void ClearAllTables()
        {
            ds.Clear();
            ds.Relations.Clear();
            ds.Tables.Clear();
            //for (int i = 0; i < ds.Relations.Count; i++)
            //{
            //    ds.clea.Relations[i].ce
            //}
            //ds.Tables.Clear();
        }

        public DataTable SelectMaster(string sqlStr, string tableName)
        {
            DataTable dt = null;
            mySqlCmd.CommandText = sqlStr;
            mySqlDataAdapter = new MySqlDataAdapter(mySqlCmd);
            mySqlDataAdapter.TableMappings.Add("Table", tableName);
            mySqlDataAdapter.Fill(ds);
            dt = ds.Tables[tableName];
            return dt;
        }

        public void SelectMoreDataMaster(string sqlStr, string tableName)
        {
            DataTable dt = ds.Tables[tableName];
            mySqlCmd.CommandText = sqlStr;
            mySqlDataAdapter.SelectCommand = mySqlCmd;
            mySqlDataAdapter.Fill(dt);
        }

        public void GenerateCommandBuilder(string sqlStr)
        {
            MySqlDataAdapter myAdapter = new MySqlDataAdapter(sqlStr, this.mySqlConn);
            mySqlCmdBuilder = new MySqlCommandBuilder(myAdapter);
            mySqlDataAdapter.InsertCommand = mySqlCmdBuilder.GetInsertCommand();
            mySqlDataAdapter.DeleteCommand = mySqlCmdBuilder.GetDeleteCommand();
            mySqlDataAdapter.UpdateCommand = mySqlCmdBuilder.GetUpdateCommand();
        }

        public void SetInserCommandBuilder(string sqlStr)
        {
            MySqlCommand mySqlCmd = new MySqlCommand(sqlStr, this.mySqlConn);
            mySqlDataAdapter.InsertCommand = mySqlCmd;
        }

        public void SetUpdateCommandBuilder(string sqlStr)
        {
            MySqlCommand mySqlCmd = new MySqlCommand(sqlStr, this.mySqlConn);
            mySqlDataAdapter.UpdateCommand = mySqlCmd;
        }

        public void SetDeleteCommandBuilder(string sqlStr)
        {
            MySqlCommand mySqlCmd = new MySqlCommand(sqlStr, this.mySqlConn);
            mySqlDataAdapter.DeleteCommand = mySqlCmd;
        }

        public void SetPrimaryKey(string tableName, DataColumn []col)
        {
            ds.Tables[tableName].PrimaryKey = col;
        }

        public DataTable SelectOther(string sqlStr, string tableName)
        {
            DataTable dt = null;
            MySqlDataAdapter myAdapter = new MySqlDataAdapter(sqlStr, this.mySqlConn);
            myAdapter.TableMappings.Add("Table", tableName);
            myAdapter.Fill(ds);
            dt = ds.Tables[tableName];
            return dt;
        }

        public void SetRelation(DataColumn col1, DataColumn col2, string relateName)
        {
            DataRelation relationShip = new DataRelation(relateName, col1, col2);
            ds.Relations.Add(relationShip);
        }

        public bool Update(string tableName)
        {
            bool isRet = true;
            int iRet = mySqlDataAdapter.Update(ds, tableName);
            
            return isRet;
        }
    }
}
